package org.hpin.asset.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.AssetDamageInfo;
import org.hpin.asset.entity.Assetinfo;
import org.hpin.asset.entity.Borrowinfo;
import org.hpin.asset.service.AssetinfoService;
import org.hpin.asset.service.BorrowinfoService;
import org.hpin.asset.util.Duuid;
import org.hpin.base.usermanager.entity.Dept;
import org.hpin.base.usermanager.service.DeptService;
import org.hpin.common.core.SpringTool;
/**
 * 
 * @author Marver
 *2015-7-10上午9:19:46
 */
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;
@Namespace("/borrowinfo")
@Action("borrowinfoAction")
@Results({
	@Result(name="listBorrowinfo" ,location = "/WEB-INF/borrowinfo/listBorrowinfo.jsp"),
	@Result(name="addBorrowinfo" ,location = "/WEB-INF/borrowinfo/addBorrowinfo.jsp"),
	@Result(name="browBorrowinfo", location = "/WEB-INF/borrowinfo/browBorrowinfo.jsp"),
	@Result(name="modifyBorrowinfo", location = "/WEB-INF/borrowinfo/modifyBorrowinfo.jsp")
	})
public class BorrowinfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6535167431978406640L;
	private Assetinfo assetinfo;
	private Borrowinfo borrowinfo;
	private BorrowinfoService borrowinfoService=(BorrowinfoService) SpringTool.getBean(BorrowinfoService.class);
	private AssetinfoService assetinfoService=(AssetinfoService) SpringTool.getBean(AssetinfoService.class);
	private AssetinfoAction assetinfoAction =new AssetinfoAction();
	private DeptService deptService = (DeptService) SpringTool
			.getBean(DeptService.class);
	
	
	/**
	 * 查询借用信息
	 * @return
	 */
	public String listBorrowinfo() throws Exception{
		page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
		Map searchMap = super.buildSearch();
		findByPage(page, searchMap);
		return "listBorrowinfo";
	}
	public Page findByPage(Page page , Map searchMap){
		borrowinfoService.findByPage(page, searchMap);
		return page ;
	}
	
	/**
	 * 添加借用前先查询信息
	 * @return
	 */
	public String addBorrowinfo() throws Exception{
		HttpTool.setAttribute("navTabId", HttpTool.getParameter("navTabId"));
		List<Dept> deptList = deptService.findAllDept();
		if(StrUtils.isNotNullOrBlank(id)){
			borrowinfo=(Borrowinfo) borrowinfoService.findById(id);
			HttpTool.setAttribute("id", id);
			String id=borrowinfo.getAssetno();
			assetinfo=(Assetinfo) assetinfoService.findById(id);
			return "modifyBorrowinfo";
		}else{
			borrowinfo =new Borrowinfo();
			borrowinfo.setDeptList(deptList);
			page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
			Map searchMap = super.buildSearch();
			searchMap.put("filter_and_state_EQ_S", "1010101");
			searchMap.put("filter_and_isDeleted_EQ_I",0);
			assetinfoAction.findByPage(page, searchMap);
			return "addBorrowinfo";
		}
	}
	
	//添加资产借用信息保存到数据库
	public String saveBorrowinfo(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			assetinfo= (Assetinfo) assetinfoService.findById(id);
			HttpTool.getRequest().setAttribute("assetInfo", assetinfo);
			assetinfo.setState(borrowinfo.getState());
			assetinfo.setDepartment(borrowinfo.getDepartment());
			assetinfo.setDeptId(borrowinfo.getDeptId());
			borrowinfo.setAssetno(id);
			borrowinfo.setDeviceName(assetinfo.getName());
			/*String dept=HttpTool.getParameter("dept");
			String []depts=dept.split(",");
			borrowinfo.setDeptId(depts[0]);
			borrowinfo.setDepartment(depts[1]);*/
			borrowinfo.setCreateTime(new Date());
			
			Borrowinfo borrowInfo= (Borrowinfo)borrowinfoService.findMax();
			if(borrowinfoService.findMax()==null){
				borrowinfo.setBorrowNo(Duuid.oddNumber(null));
			}else{
				borrowinfo.setBorrowNo(Duuid.oddNumber(borrowInfo.getBorrowNo()));
			}
			
			borrowinfoService.save(borrowinfo);
			borrowinfoService.update(assetinfo);
			HttpTool.getRequest().setAttribute("borrowInfo", borrowinfo);
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("currentTabId", currentTabId);
			json.accumulate("callbackType", "closeCurrent");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
	}
	
	/**
	 * 查询资产借用详细信息
	 * @return
	 */
	public String browBorrowinfo(){
		borrowinfo=(Borrowinfo) borrowinfoService.findById(id);
		String id=borrowinfo.getAssetno();
		assetinfo=(Assetinfo) assetinfoService.findById(id);
		return "browBorrowinfo";
	}

	/**
	 * 修改借用资产信息
	 * @return
	 */
	public String updateBorrowinfo(){
		JSONObject json = new JSONObject();
		try {
			assetinfo=(Assetinfo) assetinfoService.findById(borrowinfo.getAssetno());
			HttpTool.getRequest().setAttribute("assetInfo", assetinfo);
			assetinfo.setState(borrowinfo.getState());
			String state=borrowinfo.getState();
			if(state.equals("1010101")){
				assetinfo.setDepartment(null);
				System.out.println("状态："+borrowinfo.getState());
				assetinfo.setPrincipal(null);
			}
			assetinfoService.update(assetinfo);
			
			borrowinfoService.update(borrowinfo);
			HttpTool.getRequest().setAttribute("borrowInfo", borrowinfo);
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("callbackType", "closeCurrent");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
	}
	
	/**
	 * 删除借用信息
	 * @return
	 */
	public String deleteBorrowinfo(){
		JSONObject json = new JSONObject();
		try {
			String[] id = ids.replaceAll(" ", "").split(",");
			
			borrowinfoService.deleteMSG(id);
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("callbackType", "");
			json.accumulate("forwardUrl", "");
			json.accumulate("confirmMsg", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
	}
	
	public Borrowinfo getBorrowinfo() {
		return borrowinfo;
	}
	public void setBorrowinfo(Borrowinfo borrowinfo) {
		this.borrowinfo = borrowinfo;
	}
	public Assetinfo getAssetinfo() {
		return assetinfo;
	}
	public void setAssetinfo(Assetinfo assetinfo) {
		this.assetinfo = assetinfo;
	}


}
